package com.yiren.dbaa.module.exam.view.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.utils.DateUtil;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.module.exam.bean.ExamGradeBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by WANGRONGXIANG
 * on 2022/3/1
 * Describe 滴驾页面扣分列表适配器
 */
public class DjGradesAdapter extends BaseAdapter {

    private Context context;
    private List<ExamGradeBean> testRecords;
    private int mode;   //1:白天模式   2:夜间模式
    private OnDeleteGradeListener onDeleteGradeListener;

    public DjGradesAdapter(Context context, List<ExamGradeBean> testRecords, int mode, OnDeleteGradeListener onDeleteGradeListener) {
        this.context = context;
        this.testRecords = testRecords;
        this.mode = mode;
        this.onDeleteGradeListener = onDeleteGradeListener;
    }

    @Override
    public int getCount() {
        return testRecords.size();
    }

    @Override
    public Object getItem(int position) {
        return testRecords.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_dj_grades, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (mode == 1) {
            holder.tvProject.setTextColor(context.getResources().getColor(R.color.red));
            holder.tvCause.setTextColor(context.getResources().getColor(R.color.red));
            holder.tvScore.setTextColor(context.getResources().getColor(R.color.red));
            holder.tvTime.setTextColor(context.getResources().getColor(R.color.red));
            holder.tvAction.setTextColor(context.getResources().getColor(R.color.c1575FF));
        } else {
            holder.tvProject.setTextColor(context.getResources().getColor(R.color.cFF4040));
            holder.tvCause.setTextColor(context.getResources().getColor(R.color.cFF4040));
            holder.tvScore.setTextColor(context.getResources().getColor(R.color.cFF4040));
            holder.tvTime.setTextColor(context.getResources().getColor(R.color.cFF4040));
            holder.tvAction.setTextColor(context.getResources().getColor(R.color.c4DD7FF));
        }
        if (position < testRecords.size()) {
            GradeStandardEntity gradeStandardEntity = testRecords.get(position).getGradeStandardEntity();
            holder.tvProject.setText(StringUtil.getProjectName(gradeStandardEntity.getProjectEntity().getName()));
            if (gradeStandardEntity.getCode() == null) {
                holder.tvCause.setText(gradeStandardEntity.getStandardContent());
            } else {
                holder.tvCause.setText(gradeStandardEntity.getStandardContent() + "(" + gradeStandardEntity.getCode() + ")");
            }
            holder.tvScore.setText(gradeStandardEntity.getGrade() + "");
            holder.tvTime.setText(DateUtil.date2String(testRecords.get(position).getCreateTime(), DateUtil.LONG_TIME_FORMAT));
            //s删除扣分
            holder.tvAction.setOnClickListener(v -> {
                if (onDeleteGradeListener != null) {
                    onDeleteGradeListener.onDeleteGrade(position);
                }
            });
        }
        return convertView;
    }

    public void setMode(int mode) {
        this.mode = mode;
        notifyDataSetChanged();
    }

    static class ViewHolder {
        @BindView(R.id.tv_project)
        TextView tvProject;
        @BindView(R.id.tv_cause)
        TextView tvCause;
        @BindView(R.id.tv_score)
        TextView tvScore;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_action)
        TextView tvAction;
        @BindView(R.id.ll_grade_item)
        LinearLayout llGradeItem;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    /**
     * 删除扣分监听器
     */
    public interface OnDeleteGradeListener {

        void onDeleteGrade(int position);
    }

}
